
// app/service/news.js
module.exports= app => {
    class NewsService extends app.Service{
        * list(page = 1){
            console.log(`app/service/news.js >>> page: ${page}`);
            // read config
            const {serverUrl,pageSize} = this.app.config.news;
            // user build-in http client to GET hacker-news api
            const {data:idList} = yield this.ctx.curl(`${serverUrl}/topstories.json`,{
                data:{
                    orderBy:`"$key"`,
                    startAt: `"${pageSize * (page-1)}"`,
                    endAt: `"${pageSize * page -1}"`,
                },
                dataType:'json',
            });

            console.log(`data: `+ JSON.stringify(idList));
            console.log(``);
            console.log(``);

            const newsList = yield Object.keys(idList).map(key=>{
                const url = `${serverUrl}/item/${idList[key]}.json`;
                return this.ctx.curl(url,{dataType:'json'});
            });
            console.log(`newsList: `+ JSON.stringify(newsList));
            return newsList.map(res=>res.data);


        }
    }
    return NewsService;
}